// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.randomizedWorld.randomizedZoneStory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import zombie.core.Rand;
import zombie.iso.IsoChunk;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoMetaGrid;
import zombie.iso.IsoMovingObject;
import zombie.iso.IsoObject;
import zombie.iso.IsoWorld;
import zombie.iso.objects.IsoDeadBody;
import zombie.network.GameServer;
import zombie.network.ServerMap;
import zombie.randomizedWorld.RandomizedWorldBase;
import zombie.util.Type;

public class RandomizedZoneStoryBase extends RandomizedWorldBase {
    public boolean alwaysDo = false;
    public static final int baseChance = 15;
    public static int totalChance = 0;
    public static final String zoneStory = "ZoneStory";
    public int chance = 0;
    protected int minZoneWidth = 0;
    protected int minZoneHeight = 0;
    public final ArrayList<String> zoneType = new ArrayList();
    private static final HashMap<RandomizedZoneStoryBase, Integer> rzsMap = new HashMap();

    public static boolean isValidForStory(IsoMetaGrid.Zone zone, boolean force) {
        if (zone.pickedXForZoneStory > 0 && zone.pickedYForZoneStory > 0 && zone.pickedRZStory != null && checkCanSpawnStory(zone, force)) {
            zone.pickedRZStory.randomizeZoneStory(zone);
            zone.pickedRZStory = null;
            zone.pickedXForZoneStory = 0;
            zone.pickedYForZoneStory = 0;
        }

        if (!force && zone.hourLastSeen != 0) {
            return false;
        } else if (!force && zone.haveConstruction) {
            return false;
        } else if ("ZoneStory".equals(zone.type)) {
            doRandomStory(zone);
            return true;
        } else {
            return false;
        }
    }

    public static void initAllRZSMapChance(IsoMetaGrid.Zone zone) {
        totalChance = 0;
        rzsMap.clear();

        for (int _int = 0; _int < IsoWorld.instance.getRandomizedZoneList().size(); _int++) {
            RandomizedZoneStoryBase randomizedZoneStoryBase = (RandomizedZoneStoryBase)IsoWorld.instance.getRandomizedZoneList().get(_int);
            if (randomizedZoneStoryBase.isValid(zone, false) && randomizedZoneStoryBase.isTimeValid(false)) {
                totalChance = totalChance + randomizedZoneStoryBase.chance;
                rzsMap.put(randomizedZoneStoryBase, randomizedZoneStoryBase.chance);
            }
        }
    }

    public boolean isValid(IsoMetaGrid.Zone zone, boolean force) {
        boolean _boolean = false;

        for (int _int = 0; _int < this.zoneType.size(); _int++) {
            if (((String)this.zoneType.get(_int)).equals(zone.name)) {
                _boolean = true;
                break;
            }
        }

        return _boolean && zone.w >= this.minZoneWidth && zone.h >= this.minZoneHeight;
    }

    private static boolean doRandomStory(IsoMetaGrid.Zone zone) {
        zone.hourLastSeen++;
        byte _byte = 6;
        switch (zombie.SandboxOptions.instance.ZoneStoryChance.getValue()) {
            case 1:
                return false;
            case 2:
                _byte = 2;
            case 3:
            default:
                break;
            case 4:
                _byte = 12;
                break;
            case 5:
                _byte = 20;
                break;
            case 6:
                _byte = 40;
        }

        RandomizedZoneStoryBase randomizedZoneStoryBase0 = null;

        for (int int0 = 0; int0 < IsoWorld.instance.getRandomizedZoneList().size(); int0++) {
            RandomizedZoneStoryBase randomizedZoneStoryBase1 = (RandomizedZoneStoryBase)IsoWorld.instance.getRandomizedZoneList().get(int0);
            if (randomizedZoneStoryBase1.alwaysDo && randomizedZoneStoryBase1.isValid(zone, false) && randomizedZoneStoryBase1.isTimeValid(false)) {
                randomizedZoneStoryBase0 = randomizedZoneStoryBase1;
            }
        }

        if (randomizedZoneStoryBase0 != null) {
            int int1 = zone.x;
            int int2 = zone.y;
            int int3 = zone.x + zone.w - randomizedZoneStoryBase0.minZoneWidth / 2;
            int int4 = zone.y + zone.h - randomizedZoneStoryBase0.minZoneHeight / 2;
            zone.pickedXForZoneStory = Rand.Next(int1, int3 + 1);
            zone.pickedYForZoneStory = Rand.Next(int2, int4 + 1);
            zone.pickedRZStory = randomizedZoneStoryBase0;
            return true;
        } else if (Rand.Next(100) < _byte) {
            initAllRZSMapChance(zone);
            randomizedZoneStoryBase0 = getRandomStory();
            if (randomizedZoneStoryBase0 == null) {
                return false;
            } else {
                int int5 = zone.x;
                int int6 = zone.y;
                int int7 = zone.x + zone.w - randomizedZoneStoryBase0.minZoneWidth / 2;
                int int8 = zone.y + zone.h - randomizedZoneStoryBase0.minZoneHeight / 2;
                zone.pickedXForZoneStory = Rand.Next(int5, int7 + 1);
                zone.pickedYForZoneStory = Rand.Next(int6, int8 + 1);
                zone.pickedRZStory = randomizedZoneStoryBase0;
                return true;
            }
        } else {
            return false;
        }
    }

    public IsoGridSquare getRandomFreeSquare(RandomizedZoneStoryBase rzs, IsoMetaGrid.Zone zone) {
        IsoGridSquare isoGridSquare = null;

        for (int int0 = 0; int0 < 1000; int0++) {
            int int1 = Rand.Next(zone.pickedXForZoneStory - rzs.minZoneWidth / 2, zone.pickedXForZoneStory + rzs.minZoneWidth / 2);
            int int2 = Rand.Next(zone.pickedYForZoneStory - rzs.minZoneHeight / 2, zone.pickedYForZoneStory + rzs.minZoneHeight / 2);
            isoGridSquare = this.getSq(int1, int2, zone.z);
            if (isoGridSquare != null && isoGridSquare.isFree(false)) {
                return isoGridSquare;
            }
        }

        return null;
    }

    public IsoGridSquare getRandomFreeSquareFullZone(RandomizedZoneStoryBase rzs, IsoMetaGrid.Zone zone) {
        IsoGridSquare isoGridSquare = null;

        for (int int0 = 0; int0 < 1000; int0++) {
            int int1 = Rand.Next(zone.x, zone.x + zone.w);
            int int2 = Rand.Next(zone.y, zone.y + zone.h);
            isoGridSquare = this.getSq(int1, int2, zone.z);
            if (isoGridSquare != null && isoGridSquare.isFree(false)) {
                return isoGridSquare;
            }
        }

        return null;
    }

    private static RandomizedZoneStoryBase getRandomStory() {
        int int0 = Rand.Next(totalChance);
        Iterator iterator = rzsMap.keySet().iterator();
        int int1 = 0;

        while (iterator.hasNext()) {
            RandomizedZoneStoryBase randomizedZoneStoryBase = (RandomizedZoneStoryBase)iterator.next();
            int1 += rzsMap.get(randomizedZoneStoryBase);
            if (int0 < int1) {
                return randomizedZoneStoryBase;
            }
        }

        return null;
    }

    private static boolean checkCanSpawnStory(IsoMetaGrid.Zone zone, boolean var1) {
        int int0 = zone.pickedXForZoneStory - zone.pickedRZStory.minZoneWidth / 2 - 2;
        int int1 = zone.pickedYForZoneStory - zone.pickedRZStory.minZoneHeight / 2 - 2;
        int int2 = zone.pickedXForZoneStory + zone.pickedRZStory.minZoneWidth / 2 + 2;
        int int3 = zone.pickedYForZoneStory + zone.pickedRZStory.minZoneHeight / 2 + 2;
        int int4 = int0 / 10;
        int int5 = int1 / 10;
        int int6 = int2 / 10;
        int int7 = int3 / 10;

        for (int int8 = int5; int8 <= int7; int8++) {
            for (int int9 = int4; int9 <= int6; int9++) {
                IsoChunk isoChunk = GameServer.bServer ? ServerMap.instance.getChunk(int9, int8) : IsoWorld.instance.CurrentCell.getChunk(int9, int8);
                if (isoChunk == null || !isoChunk.bLoaded) {
                    return false;
                }
            }
        }

        return true;
    }

    public void randomizeZoneStory(IsoMetaGrid.Zone zone) {
    }

    public boolean isValid() {
        return true;
    }

    public void cleanAreaForStory(RandomizedZoneStoryBase rzs, IsoMetaGrid.Zone zone) {
        int int0 = zone.pickedXForZoneStory - rzs.minZoneWidth / 2 - 1;
        int int1 = zone.pickedYForZoneStory - rzs.minZoneHeight / 2 - 1;
        int int2 = zone.pickedXForZoneStory + rzs.minZoneWidth / 2 + 1;
        int int3 = zone.pickedYForZoneStory + rzs.minZoneHeight / 2 + 1;

        for (int int4 = int0; int4 < int2; int4++) {
            for (int int5 = int1; int5 < int3; int5++) {
                IsoGridSquare isoGridSquare = IsoWorld.instance.getCell().getGridSquare(int4, int5, zone.z);
                if (isoGridSquare != null) {
                    isoGridSquare.removeBlood(false, false);

                    for (int int6 = isoGridSquare.getObjects().size() - 1; int6 >= 0; int6--) {
                        IsoObject isoObject0 = isoGridSquare.getObjects().get(int6);
                        if (isoGridSquare.getFloor() != isoObject0) {
                            isoGridSquare.RemoveTileObject(isoObject0);
                        }
                    }

                    for (int int7 = isoGridSquare.getSpecialObjects().size() - 1; int7 >= 0; int7--) {
                        IsoObject isoObject1 = (IsoObject)isoGridSquare.getSpecialObjects().get(int7);
                        isoGridSquare.RemoveTileObject(isoObject1);
                    }

                    for (int int8 = isoGridSquare.getStaticMovingObjects().size() - 1; int8 >= 0; int8--) {
                        IsoDeadBody isoDeadBody = Type.tryCastTo((IsoMovingObject)isoGridSquare.getStaticMovingObjects().get(int8), IsoDeadBody.class);
                        if (isoDeadBody != null) {
                            isoGridSquare.removeCorpse(isoDeadBody, false);
                        }
                    }

                    isoGridSquare.RecalcProperties();
                    isoGridSquare.RecalcAllWithNeighbours(true);
                }
            }
        }
    }

    public int getMinimumWidth() {
        return this.minZoneWidth;
    }

    public int getMinimumHeight() {
        return this.minZoneHeight;
    }

    public static enum ZoneType {
        Forest,
        Beach,
        Lake,
        Baseball,
        MusicFestStage,
        MusicFest,
        NewsStory;
    }
}
